package postprocessing;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Vector;

import org.apache.lucene.analysis.shingle.ShingleMatrixFilter.Matrix.Column;

public class SumUpResults {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String pathname = "/local/joofeit/resultsSorted";
		File dir = new File(pathname);
		int column =0;
		SortedMap<String, String[]> map = new TreeMap<String, String[]>();
		if (dir.isDirectory()){
			File[] files = dir.listFiles();
			Collections.sort(Arrays.asList(files));
			for (File f: files){
				
				System.out.println(f.getName());
				String name = f.getName().split("__")[0].substring(8);
				System.out.println(name);
				column = setColumn(column, name);
				BufferedReader br = new BufferedReader(new FileReader(f));
				String line ="";
				int counter = 0;
				String[] list = new String[30];
				while ((line = br.readLine())!= null&&counter <30 ){
					String[] lineSplit = line.split(" ");
					String entry = lineSplit[0].substring(0,8)+" "+lineSplit[1];
					list[counter] = entry;
					column = setColumn(column, entry);
					counter++;
					
				}
				map.put(name, list);
				br.close();
				
			}
			makeTable(map, column);
			
		}
	}
	
	public static int setColumn (int column , String str){
		if (str.length()> column){
			column= str.length();
		}
		return column;
	}
	public static void makeTable(SortedMap<String, String[]> map, int column) throws IOException{
	String format ="";
	Object[] args = new String[map.keySet().size()];
	BufferedWriter bw = new BufferedWriter(new FileWriter(new File("/local/joofeit/ResultsALL")));
	int counter =0;
	for(int y= 0; y<3 ;y++){
		format = format+" %-"+(column+3)+"s";
		//args[counter] = idioms;
		counter++;
	}
	System.out.println(format.substring(1).split(",").length);
	System.out.println("\""+format.substring(1)+"\"");


	
	//String line = String.format(format.substring(1),args);
	//bw.write(line+"\n");
	//System.out.println(line);
	Object[] keys = map.keySet().toArray();
	int start=0;
	int end = 2;
	while(end < keys.length){
	String[] idioms = new String[3];
	for(int c =0; c <3;c++){
		idioms[c] = (String)keys[start];
		start++;
	}
	String line1 = String.format(format.substring(1),idioms);
	bw.write(line1+"\n");
	
	start -=3;
	for(int x =0;x<30;x++){
		Object[] curArgs = new String[3];
		int idiomPointer =0;
		
		while (start <=end){
			if(x+1<=10){
			String cur =" "+Integer.toString(x)+". "+map.get(keys[start])[x];
			curArgs[idiomPointer] = cur;

			}else{
			String cur =Integer.toString(x)+". "+map.get(keys[start])[x];
			curArgs[idiomPointer] = cur;

			}
			idiomPointer++;
			start++;
			

		}
		String line2 = String.format(format.substring(1),curArgs);
		bw.write(line2+"\n");
		start -=3;
		
	}
	bw.write("----------------------------------------------------------------------------------------------");
	bw.write("\n"+"\n");
	start +=3;
	end+=3;
	}
	bw.flush();
	bw.close();
	}

}
